package sk.uniba.fmph.asdf.db;

import java.util.Objects;
import java.util.regex.Pattern;

import sk.uniba.fmph.asdf.SerializableEntity;

/**
 * Trieda reprezentuje cele meno uzivatela
 * @serial
 *
 * @author tim
 */
public class FullName extends SerializableEntity {
	private static final long serialVersionUID = -8923642114359337792L;

	private static final Pattern validName = Pattern.compile("\\a{2,}");

    /**
     * krstne meno
     * @serialField
     */
    private String firstName;

    /**
     * priezvisko
     * @serialField
     */
    private String lastName;

    @Override
    public void validateState() {
        validateFirstName(firstName);
        validateLastName(lastName);
    }

    /**
     * Metoda overi spravnost krstneho mena.
     * @param firstName krstne meno osoby
     */
    private void validateFirstName(final String firstName) {
        if (!validName.matcher(firstName).matches()) {
            throw new IllegalArgumentException(lastName);
        }
    }

    /**
     * Metoda overi spravnost priezviska.
     * @param lastName priezvisko osoby
     */
    private void validateLastName(final String lastName) {
        if (!validName.matcher(lastName).matches()) {
            throw new IllegalArgumentException(lastName);
        }
    }

    public FullName(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    /**
     * Vrati krstne meno.
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Vrati priezvisko.
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Vrati cele meno v retazci.
     */
    @Override
    public String toString() {
        return firstName + " " + lastName;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof FullName) {
            FullName fullName = (FullName) o;
            return (fullName.firstName.equals(firstName)) && (fullName.lastName.equals(lastName));
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 29 * hash + Objects.hashCode(this.firstName);
        hash = 29 * hash + Objects.hashCode(this.lastName);
        return hash;
    }

}
